package com.kye.express.business.customer_intro.withdraw;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.customer_intro.detail.CustomerIntroDetailActivity;
import com.kye.express.databinding.ItemCustomerIntroHistoryBinding;
import com.kye.express.databinding.ItemCustomerIntroWithdrawBinding;
import com.kye.model.bean.customer_intro.CustomerIntroHistoryResultBean;

import java.text.DecimalFormat;

import io.reactivex.functions.Consumer;

class CustomerIntroWithdrawListItemViewBinder extends me.drakeet.multitype.ItemViewBinder<CustomerIntroHistoryResultBean.DatasBean, CustomerIntroWithdrawListItemViewBinder.ViewHolder> {

    private Context mContext;
    private final DecimalFormat mDecimalFormat = new DecimalFormat("##0.00");
    private CustomerIntroWithdrawActivityContract.CustomerIntroWithdrawActivityView mCustomerIntroWithdrawActivityView;

    public CustomerIntroWithdrawListItemViewBinder(Context context,CustomerIntroWithdrawActivityContract.CustomerIntroWithdrawActivityView view) {
        mContext = context;
        mCustomerIntroWithdrawActivityView = view;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_customer_intro_withdraw, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull CustomerIntroHistoryResultBean.DatasBean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private CustomerIntroHistoryResultBean.DatasBean data;
        ItemCustomerIntroWithdrawBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(CustomerIntroHistoryResultBean.DatasBean item) {
            data = item;
            if (data != null) {
                dataBinding.setData(data);
                dataBinding.checkbox.setChecked(data.isChecked());
                dataBinding.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        data.setChecked(b);
                        if (mCustomerIntroWithdrawActivityView != null){
                            mCustomerIntroWithdrawActivityView.onItemCheckChanged();
                        }
                    }
                });
                String format = mDecimalFormat.format(data.getCashAmount());
                dataBinding.tvMoney.setText("可提现：￥" + format);
            }
        }
    }
}
